#ifndef MISCFUNC
#define MISCFUNC

#include "opcomplx.h"

double pi(void)
{
	return 3.14159265359;
}

double r2d(double rad)
{
	double angle;
	angle=rad*180/pi();
	angle=fmod(angle,360);
	if(angle>180) angle-=360;
	return(angle);
}

double d2r(double deg)
{
	return(deg*pi()/180);
}


/*
void drawvect(complex a, complex org, double scale)
{
	double mag,ang;
	double posx, posy;

	posx=a.x+org.x;
	posy=a.y+org.y;
	mag=a.abs();
	ang=a.phas(1);

	glBegin(GL_LINES);
		glVertex2f(org.x*scale,org.y*scale);
		glVertex2f(posx*scale,posy*scale);
	glEnd();

	if(mag*scale > 15)
	{
		mag=12/scale;
		glBegin(GL_LINES);
			glVertex2f(posx*scale,posy*scale);
			glVertex2f((posx-mag*cos((ang-10)*pi()/180))*scale,(posy-mag*sin((ang-10)*pi()/180))*scale);
			glVertex2f(posx*scale,posy*scale);
			glVertex2f((posx-mag*cos((ang+10)*pi()/180))*scale,(posy-mag*sin((ang+10)*pi()/180))*scale);
		glEnd();
	}
}
*/
void plotvect(FILE *arq, complex a, complex org, double scale)
{
	double mag,ang;
	double posx, posy;

	posx=a.x+org.x;
	posy=a.y+org.y;
	mag=a.abs();
	ang=a.phas(1);

	fprintf(arq, "newpath\n");
	fprintf(arq, "\t%lf %lf moveto\n", org.x*scale, org.y*scale);
	fprintf(arq, "\t%lf %lf lineto\n", posx*scale, posy*scale);
	fprintf(arq, "closepath\n");
	fprintf(arq, "stroke\n");
		
	if(mag*scale > 15)
	{
		mag=12/scale;
		fprintf(arq, "newpath\n");
		fprintf(arq, "\t%lf %lf moveto\n", posx*scale,posy*scale );
		fprintf(arq, "\t%lf %lf lineto\n", (posx-mag*cos((ang-10)*pi()/180))*scale,(posy-mag*sin((ang-10)*pi()/180))*scale);
		//fprintf(arq, "\t%lf %lf moveto\n", posx*scale,posy*scale );
		fprintf(arq, "\t%lf %lf lineto\n", (posx-mag*cos((ang+10)*pi()/180))*scale,(posy-mag*sin((ang+10)*pi()/180))*scale);
		fprintf(arq, "closepath\n");
		fprintf(arq, "fill\n");
		fprintf(arq, "stroke\n");
	}
}

void epsheader(FILE *arq, double tamx, double tamy)
{
	// Cabecalho do Encapsulated Postscript
	fprintf(arq,"%%!PS-Adobe EPSF-3.0\n");
	fprintf(arq,"%%%%BoundingBox: %f %f %f %f\n", ((598.6/2)-(520.0/2))-20, ((841.9/2)-(520.0/2))-20, ((598.6/2)-(520.0/2)+520)+20, ((841.9/2)-(520.0/2)+520)+20);
	fprintf(arq,"%%%%Pages: 1\n");

	// Assegurar o salvamento do estado do PostScript atual
	fprintf(arq,"gsave\n");

	// Macro para conversao pontos-tela em pontos-impressora
	fprintf(arq,"/pt {520 %lf div mul} def\n", tamx);
	
	//Deslocamento do centro de coordenadas
	fprintf(arq,"%lf %lf translate\n", (598.6/2)-(520.0/2), (841.9/2)-(520.0/2));
}

void epsfooter(FILE *arq)
{
	// Rodape do Encapsulated Postscript
	fprintf(arq,"grestore\n");
	fprintf(arq,"%%%%EOF\n");
}


void epsfunc(FILE *arq)
{
	fprintf(arq,"\n/showlefttext\n{\n\tmoveto\n\tshow\n} bind def\n");
	fprintf(arq,"\n/showtext\n{\n\tshowlefttext\n} bind def\n");
	fprintf(arq,"\n/showcentertext\n");
	fprintf(arq,"{\n");
	fprintf(arq,"\tgsave\n");
	fprintf(arq,"\t/yi exch def\n");
	fprintf(arq,"\t/xi exch def\n");
	fprintf(arq,"\t/texto exch def\n");
	fprintf(arq,"\t0 setlinewidth\n");
	fprintf(arq,"\tnewpath\n");
	fprintf(arq,"\t0 0 moveto\n");
	fprintf(arq,"\ttexto false charpath\n");
	fprintf(arq,"\tflattenpath\n");
	fprintf(arq,"\tpathbbox\n");
	fprintf(arq,"\t/y2 exch def\n");
	fprintf(arq,"\t/x2 exch def\n");
	fprintf(arq,"\t/y1 exch def\n");
	fprintf(arq,"\t/x1 exch def\n");
	fprintf(arq,"\tx2 x1 sub 2 div -1 mul\n");
	fprintf(arq,"\t/offx exch def\n");
	fprintf(arq,"\ttexto xi offx add yi showtext\n");
	fprintf(arq,"\tgrestore\n");
	fprintf(arq,"} bind def\n\n");
	
	fprintf(arq,"\n/showcenteredtext\n");
	fprintf(arq,"{\n");
	fprintf(arq,"\tgsave\n");
	fprintf(arq,"\t/yi exch def\n");
	fprintf(arq,"\t/xi exch def\n");
	fprintf(arq,"\t/texto exch def\n");
	fprintf(arq,"\t0 setlinewidth\n");
	fprintf(arq,"\tnewpath\n");
	fprintf(arq,"\t0 0 moveto\n");
	fprintf(arq,"\ttexto false charpath\n");
	fprintf(arq,"\tflattenpath\n");
	fprintf(arq,"\tpathbbox\n");
	fprintf(arq,"\t/y2 exch def\n");
	fprintf(arq,"\t/x2 exch def\n");
	fprintf(arq,"\t/y1 exch def\n");
	fprintf(arq,"\t/x1 exch def\n");
	fprintf(arq,"\ty2 y1 sub 2 div -1 mul\n");
	fprintf(arq,"\t/offy exch def\n");
	fprintf(arq,"\tx2 x1 sub 2 div -1 mul\n");
	fprintf(arq,"\t/offx exch def\n");
	fprintf(arq,"\ttexto xi offx add yi offy add showtext\n");
	fprintf(arq,"\tgrestore\n");
	fprintf(arq,"} bind def\n\n");
	
	fprintf(arq,"\n/showrighttext\n");
	fprintf(arq,"{\n");
	fprintf(arq,"\tgsave\n");
	fprintf(arq,"\t/yi exch def\n");
	fprintf(arq,"\t/xi exch def\n");
	fprintf(arq,"\t/texto exch def\n");
	fprintf(arq,"\t0 setlinewidth\n");
	fprintf(arq,"\tnewpath\n");
	fprintf(arq,"\t0 0 moveto\n");
	fprintf(arq,"\ttexto false charpath\n");
	fprintf(arq,"\tflattenpath\n");
	fprintf(arq,"\tpathbbox\n");
	fprintf(arq,"\t/y2 exch def\n");
	fprintf(arq,"\t/x2 exch def\n");
	fprintf(arq,"\t/y1 exch def\n");
	fprintf(arq,"\t/x1 exch def\n");
	fprintf(arq,"\tx2 x1 sub -1 mul\n");
	fprintf(arq,"\t/offx exch def\n");
	fprintf(arq,"\ttexto xi offx add yi showtext\n");
	fprintf(arq,"\tgrestore\n");
	fprintf(arq,"} bind def\n\n");

	fprintf(arq,"\n/quad\n");
	fprintf(arq, "{\n");
	fprintf(arq, "\tnewpath\n");
	fprintf(arq, "\t/y2 exch def\n");
	fprintf(arq, "\t/x2 exch def\n");
	fprintf(arq, "\t/y1 exch def\n");
	fprintf(arq, "\t/x1 exch def\n");
	fprintf(arq, "\tx1 y1 moveto\n");
	fprintf(arq, "\tx1 y2 lineto\n");
	fprintf(arq, "\tx2 y2 lineto\n");
	fprintf(arq, "\tx2 y1 lineto\n");
	fprintf(arq, "\tx1 y1 lineto\n");
	fprintf(arq, "\tclosepath\n");
	fprintf(arq, "} bind def\n\n");

	//fprintf(arq,"/%s findfont\n",TXT_FONT_NAME_PS);
	//fprintf(arq,"%f scalefont setfont\n",(float)(TXT_FONT_SIZE_PS));
}

/*
void outtext( int style, void *font, float x, float y, char *msg)
{
	float tam=0;
	char *temp;

	temp=msg;
	while(*temp) {tam+=glutBitmapWidth(font,*temp); temp++;}
	
	switch(style)
	{
	case 0:	//left align
		glRasterPos2f(x,y);
		break;
	case 1: //center align
		glRasterPos2f(x-tam/2,y);
		break;
	case 2: //right align
		glRasterPos2f(x-tam,y);
		break;
	}
		
	while(*msg)	{glutBitmapCharacter( font, *msg); msg++;}
}

void strokeOutput(GLfloat sc, GLfloat x, GLfloat y, char *format)
{
  char *p;
  GLfloat scx, scy, scz;
  int w;

  scx=scy=scz=sc;
  
  glPushMatrix();
  glScalef(scx, scy, scz);
  for (w=0,p = format; *p; p++)
    w+=0.1*glutStrokeWidth(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();

  glPushMatrix();
  glTranslatef(x-w/2, y, 0);
  glScalef(scx, scy, scz);
  for (p = format; *p; p++)
    glutStrokeCharacter(GLUT_STROKE_ROMAN, *p);
  glPopMatrix();
}
*/
class matmem
{
public:
	float sn[361];
	float cs[361];
	void init();
};

void matmem::init()
{
	int i;
	float pi;
	pi=(float)(4*atan(1));
	for (i=0;i<=360;i++)
	{
		sn[i]=(float)sin((float)i*pi/180);
		cs[i]=(float)cos((float)i*pi/180);
	}
}

#define CIRCLE 0
#define DISK 1
/*
void circle(int style, float rad)
{
	int i;
	extern matmem m;

	if(style==0) glBegin(GL_LINE_LOOP);
	else glBegin(GL_TRIANGLE_FAN);
		for(i=0;i<360;i+=10)		
			glVertex2f(rad*m.cs[i],rad*m.sn[i]);
	glEnd();
}
*/

char sinal(int num)
{
	return ( (num<=0) ? -1 : 1);
}

#endif
